package com.gitee.morilys.jsmile.gen;


import com.gitee.morilys.jsmile.gen.db.DBUtils;
import com.gitee.morilys.jsmile.gen.gentable.GenTables;
import com.gitee.morilys.jsmile.gen.gentable.Table;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenKit {


	public static Configuration cfg = null;

	public static void main(String[] args) throws Exception {
		List<Table> tables = GenTables.me().getTables();
		for (Table table : tables) {
			//gen(table);
			genPlusDDD(table);
		}
	}
	/**
	 * 自动生成代码
	 */
	public static void genPlusDDD(Table table) throws Exception {
		genFile(table,"mybatisplus/EntityPlus.java.ftl",table.getEntityPackageName(),".java");
		genFile(table,"mybatisplus/MapperPlus.xml.ftl",table.getMybatisPath(),".xml");
		genFile(table,"mybatisplus/DaoPlus.java.ftl",table.getDaoPackageName(),"Dao.java");
		genFile(table,"mybatisplus/Repository.java.ftl",table.getBaseRepoPackname(),"Repository.java");
		genFile(table,"mybatisplus/SearchRepository.java.ftl",table.getBaseSearchRepoPackname(),"SearchRepository.java");
	}

	/**
	 * 自动生成代码
	 */
	public static void genPlus(Table table) throws Exception {
		genFile(table,"mybatisplus/EntityPlus.java.ftl",table.getEntityPackageName(),".java");
		genFile(table,"mybatisplus/MapperPlus.xml.ftl",table.getMybatisPath(),".xml");
		genFile(table,"mybatisplus/DaoPlus.java.ftl",table.getDaoPackageName(),"Dao.java");
		genFile(table,"mybatisplus/SearchRepository.java.ftl",table.getServicePackageName(),"Service.java");
		genFile(table,"mybatisplus/Repository.java.ftl",table.getServiceImplPackageName(),"ServiceImpl.java");
	}

	/**
	 * 自动生成代码
	 */
	public static void gen(Table table) throws Exception {
		genFile(table,"EntityPlus.java.ftl",table.getEntityPackageName(),".java");
		//genFile(table,"springboot-Dao.java.ftl",table.getDaoPackageName(),"Dao.java");
		genFile(table,"MapperPlus.xml.ftl",table.getMybatisPath(),".xml");
//		genFile(table,"EntityTo.java.ftl",table.getEntityToPackageName(),"QueryTo.java");
//		genFile(table,"EntityVo.java.ftl",table.getEntityVoPackageName(),"Vo.java");
		//genFile(table,"Service.java.ftl",table.getServicePackageName(),"Service.java");
//		genFile(table,"Controller.java.ftl",table.getControllerPackageName(),"Controller.java");

	}


	static {
		cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
		cfg.setDefaultEncoding("UTF-8");
		String filePath = GenKit.class.getClassLoader().getResource("template").getPath();
		File templateFile = new File(filePath);
		try {
			cfg.setDirectoryForTemplateLoading(templateFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 生成freemarker模板文件
	 */
	public static void genFile(Table table ,String descFtl,String packName,String suffix) throws Exception {
		String mybatisPath =  DBUtils.srcPath + packName.replace(".", File.separator)
				+ File.separatorChar;
		File mybatisFile = new File(mybatisPath);
		if (!mybatisFile.exists()) {
			mybatisFile.mkdirs();
		}
		mybatisPath += table.getClassName().trim() + suffix;
		Map<String, Object> mybatisMap = new HashMap<String, Object>();
		mybatisMap.put("model", table);
		Writer mybatisOut = new OutputStreamWriter(new FileOutputStream(mybatisPath), "UTF-8");
		Template mybatisTemplate = cfg.getTemplate(descFtl, "UTF-8");
		mybatisTemplate.process(mybatisMap, mybatisOut);
	}

}
